package cn.iocoder.yudao.module.erp.dal.mysql.pristock;

import java.math.BigDecimal;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.erp.dal.dataobject.pristock.PriStockDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.secstock.SecStockDO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.erp.controller.admin.pristock.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * ERP 一级库库存 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface PriStockMapper extends BaseMapperX<PriStockDO> {

    default PageResult<PriStockDO> selectPage(PriStockPageReqVO reqVO) {
        LambdaQueryWrapperX<PriStockDO> wrapper = new LambdaQueryWrapperX<PriStockDO>()
                .eqIfPresent(PriStockDO::getProductId, reqVO.getProductId())
                .likeIfPresent(PriStockDO::getProductName, reqVO.getProductName())
                .eqIfPresent(PriStockDO::getProductUnitName, reqVO.getProductUnitName())
                .likeIfPresent(PriStockDO::getProductStandards, reqVO.getProductStandards())
                .betweenIfPresent(PriStockDO::getProductPrice, reqVO.getProductPrice())
                .likeIfPresent(PriStockDO::getProductBatch, reqVO.getProductBatch())
                .betweenIfPresent(PriStockDO::getProductValidityDate, reqVO.getProductValidityDate())
                .betweenIfPresent(PriStockDO::getCreateTime, reqVO.getCreateTime());

        // 只有 count 不为 null 并且格式正确时才添加条件
        if (reqVO.getCount() != null
                && reqVO.getCount().length == 2
                && StringUtils.isNotBlank(reqVO.getCount()[0])
                && StringUtils.isNotBlank(reqVO.getCount()[1])) {
            wrapper.apply(
                    "count IS NOT NULL AND CAST(count AS DECIMAL(20,2)) BETWEEN {0} AND {1}",
                    new BigDecimal(reqVO.getCount()[0]),
                    new BigDecimal(reqVO.getCount()[1]));
        }

        wrapper.orderByDesc(PriStockDO::getId);

        return selectPage(reqVO, wrapper);
    }


    @Select("select * from erp_pri_stock  " +
            "where product_id = #{productId} and product_batch = #{productBatch}  " +
            "and product_validity_date = #{productValidityDate} ")
    List<PriStockDO> selectexist(PriStockSaveReqVO updateReqVO);







    @Update("update erp_pri_stock set count = #{count}  where product_id = #{productId}")
    void updatePriStocks(PriStockSaveReqVO priStockSaveReqVO);

    @Select( " select * from erp_pri_stock where product_id in #{productIds} "  )
    List<PriStockDO> getlistproduct(List<Long> productIds);

    @Select("SELECT distinct product_batch FROM erp_pri_stock where product_id = #{productId}")
    List<String> selectProductBatchsByProductId(Long productId);

    @Select("SELECT distinct product_id,product_name,product_standards FROM erp_pri_stock")
    List<PriStockRespVO> selectProductIds();

    @Update("update erp_pri_stock set count = count + #{count} where id = #{id}")
    void updatePristockCount(PriStockDO productByBatch);

    void updatePristockCountBatch(@Param("stocksToUpdate") List<PriStockDO> stocksToUpdate);

    List<PriStockDO> selectBatchByBatchs(@Param("stocks") List<Object> stocks);

    @Select("SELECT distinct product_id FROM erp_pri_stock")
    List<Integer> selectIds();

    @Select("SELECT distinct product_standards FROM erp_pri_stock where product_name = #{productName} ")
    List<String> selectStandardsByProductName(String productName);

    @Select("SELECT distinct inbound_batch FROM erp_pri_stock where product_id = #{productId} and product_batch = #{productBatch}")
    List<String> selectInboundBatchsByProductId(Long productId, String productBatch);

    @Select("SELECT product_id, product_name, product_standards, product_batch, product_unit_id, product_unit_name, product_validity_date, SUM(`count`) AS count " +
            "FROM erp_pri_stock " +
            "GROUP BY product_id, product_name, product_standards, product_batch, product_unit_id, product_unit_name, product_validity_date")
    List<PriStockDO> selectPriListByBatch();

    @Select("SELECT product_id, product_name, product_standards, product_batch, product_unit_id, product_unit_name, product_validity_date, SUM(`count`) AS count " +
            "FROM erp_pri_stock " +
            "where product_id = #{productId} and product_batch = #{productBatch}"+
            "GROUP BY product_id, product_name, product_standards, product_batch, product_unit_id, product_unit_name, product_validity_date")
    PriStockDO selectByProductIdAndBatch(Long productId, String productBatch);

    @Select("SELECT inbound_batch FROM erp_pri_stock where product_id = #{productId} and product_batch = #{productBatch} and dept_id = #{deptId} order by create_time desc limit 1")
    String selectNewInboundBatch(Long productId, String productBatch, Long deptId);

    @Select("SELECT inbound_batch FROM erp_pri_stock where product_id = #{productId} and product_batch = #{productBatch} and dept_id = #{deptId} order by create_time asc limit 1")
    String selectLastInboundBatch(Long productId, String productBatch, Long deptId);

    List<PriStockDO> selectStockListByProductBatch(List<PriStockDO> stocks);
}